package com.student;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.Student;
import com.bean.Teacher;
import com.utils.DaoFactory;

/**
 * Servlet implementation class ChangeStudentPwd
 */
@WebServlet("/ChangeStudentPwd")
public class ChangeStudentPwd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeStudentPwd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		String id=(String)session.getAttribute("stu_id");
		String oldPwd = request.getParameter("oldPwd");
		String newPwd1 = request.getParameter("newPwd1");
		String newPwd2 = request.getParameter("newPwd2");
		Student student = DaoFactory.getStudentDaoInstance().singlestudentsearch(id);
		if (student.getStu_pwd().equals(oldPwd) && newPwd1.equals(newPwd2) && newPwd1 != null) {
			student.setStu_pwd(newPwd1);
			int result=DaoFactory.getStudentDaoInstance().update(student, id);
			if(result>0)
				session.setAttribute("StudentPwd_message", "<script>alert('密码修改成功');</script>");
				response.sendRedirect("student/show_info.jsp");
		} else {			
			session.setAttribute("StudentPwd_message", "<script>alert('输入原密码错误或修改后两次密码');</script>");
			response.sendRedirect("student/show_info.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
